import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.Element;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -06 -21
 * Time: 19:41
 */


class Student{
    public int studentid;
    public int classid;
    public int id;
}


@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doput");
        resp.getWriter().write("doput");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("dopost");
        //resp.getWriter().write("dopost");
//        StringBuilder sb = new StringBuilder();
//        resp.setContentType("text/html");
//        sb.append("studentid:" + req.getParameter("studentid") + " classid:" + req.getParameter("classid") + "<br>");
//        resp.getWriter().write(sb.toString());
        ObjectMapper om=new ObjectMapper();
        Student s=om.readValue(req.getInputStream(),Student.class);
        resp.setContentType("text/html");
        //resp.getWriter().write(s.classid+ " "+ s.studentid+"<br>");
        System.out.println(s.classid+ " "+ s.studentid);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        resp.setContentType("text/html");
        System.out.println("hello word3");
        StringBuilder sb = new StringBuilder();
        sb.append(req.getProtocol());
        sb.append("<br>");
        sb.append(req.getMethod());
        sb.append("<br>");
        sb.append(req.getRequestURI());
        sb.append("<br>");
        sb.append(req.getContextPath());
        sb.append("<br>");
        sb.append(req.getQueryString());
        sb.append("<br>");
        Enumeration<String> e = req.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = e.nextElement();
            sb.append(name + " " + req.getHeader(name) + "<br>");
        }
        resp.getWriter().write(sb.toString());
    }
}
